#include<stdio.h>
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>
#include<stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
  
  pid_t  pid;
  int pipe_fd[2];
  ssize_t nums;
  char buff_r[100],buff_w[100];
  memset(buff_r,0,sizeof(buff_r));
  memset(buff_w,0,sizeof(buff_w));
  
  if(pipe(pipe_fd)==0)//管道创建成功
  {
    pid=fork();
    if(pid==0)//子进程读
    {
      close(pipe_fd[1]);//关闭写端
      nums=read(pipe_fd[0],buff_r,sizeof(buff_r));
      if(nums>0)
      {
        printf("子进程读取到的信息是：%s\n",buff_r);
      }
      close(pipe_fd[0]);
      exit(0);
    }
    //父进程写
    close(pipe_fd[0]);//关闭读端
    printf("从键盘输入信息：\n");
    scanf("%s",buff_w);
    //写入管道
    if(write(pipe_fd[1],buff_w,sizeof(buff_w))!=-1)
    {
      printf("父进程写入管道成功\n");
    }
    close(pipe_fd[1]);
    waitpid(pid,NULL,0);
    exit(0);
  }
}
